﻿using SHomeWorkshop.LunarSF.StudentInfosManager.ValidationRules;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace SHomeWorkshop.LunarSF.StudentInfosManager
{
    public class StudentBaseInfos : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 属性值发生变化时产生通知事件。
        /// </summary>
        public void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(sender, e);
            }
        }

        public override string ToString()
        {
            StringBuilder sBuilder = new StringBuilder();

            //其实可以用反射，但这样更简单些。
            sBuilder.Append($"{nameof(StudentGrade)}|{StudentGrade}\r\n");
            sBuilder.Append($"{nameof(StudentClass)}|{StudentClass}\r\n");
            sBuilder.Append($"{nameof(StudentNumber)}|{StudentNumber}\r\n");

            sBuilder.Append($"{nameof(StudentName)}|{StudentName}\r\n");
            sBuilder.Append($"{nameof(StudentOldName)}|{StudentOldName}\r\n");
            sBuilder.Append($"{nameof(StudentCid)}|{StudentCid}\r\n");

            sBuilder.Append($"{nameof(SchoolRoll)}|{SchoolRoll}\r\n");
            sBuilder.Append($"{nameof(AdmissionDate)}|{AdmissionDate}\r\n");
            sBuilder.Append($"{nameof(StudentNation)}|{StudentNation}\r\n");

            sBuilder.Append($"{nameof(PoliticalStatus)}|{PoliticalStatus}\r\n");
            sBuilder.Append($"{nameof(NativePlace)}|{NativePlace.ToString()}\r\n");
            sBuilder.Append($"{nameof(ResidencePlace)}|{ResidencePlace.ToString()}\r\n");

            sBuilder.Append($"{nameof(ResidenceType)}|{ResidenceType}\r\n");
            sBuilder.Append($"{nameof(StudentHealthCondition)}|{StudentHealthCondition}\r\n");
            sBuilder.Append($"{nameof(StudentCurrentPlace)}|{StudentCurrentPlace}\r\n");

            sBuilder.Append($"{nameof(StudentDetailAddress)}|{StudentDetailAddress}\r\n");
            sBuilder.Append($"{nameof(StudentEmail)}|{StudentEmail}\r\n");
            sBuilder.Append($"{nameof(PostCode)}|{PostCode}\r\n");

            sBuilder.Append($"{nameof(StudentPhoneCode)}|{StudentPhoneCode}\r\n");
            sBuilder.Append($"{nameof(GuardianName)}|{GuardianName}\r\n");
            sBuilder.Append($"{nameof(GuardianCompany)}|{GuardianCompany}\r\n");

            sBuilder.Append($"{nameof(GuardianCid)}|{GuardianCid}\r\n");
            sBuilder.Append($"{nameof(GuardianRelation)}|{GuardianRelation}\r\n");
            sBuilder.Append($"{nameof(GuardianPhoneCode)}|{GuardianPhoneCode}\r\n");

            //TODO: 每新加一个字段，都要在这里添加

            return sBuilder.ToString();
        }

        public string BlankErrorText
        {
            get
            {
                StringBuilder sb = new StringBuilder();

                if (string.IsNullOrWhiteSpace(StudentGrade)) sb.Append($"{nameof(StudentGrade)}（学生年级）\r\n");
                if (string.IsNullOrWhiteSpace(StudentClass)) sb.Append($"{nameof(StudentClass)}（学生班级）\r\n");
                if (string.IsNullOrWhiteSpace(StudentNumber)) sb.Append($"{nameof(StudentNumber)}（学生在班级的编号）\r\n");
                if (string.IsNullOrWhiteSpace(StudentName)) sb.Append($"{nameof(StudentName)}（学生姓名）\r\n");

                //学生曾用名，可以为空
                //if (string.IsNullOrWhiteSpace(StudentOldName)) sb.Append($"{nameof(StudentOldName)}\r\n");

                if (string.IsNullOrWhiteSpace(StudentCid)) sb.Append($"{nameof(StudentCid)}（学生身份证号）\r\n");

                //学生学籍号，可以为空
                //if (string.IsNullOrWhiteSpace(SchoolRoll)) sb.Append($"{nameof(SchoolRoll)}\r\n");

                //学生入学年份
                if (string.IsNullOrWhiteSpace(AdmissionDate)) sb.Append($"{nameof(AdmissionDate)}（学生入学年份）\r\n");

                //学生国籍，可以不填，默认为中国
                //if (string.IsNullOrWhiteSpace(StudentNation)) sb.Append($"{nameof(StudentNation)}\r\n");

                //学生政治面貌
                if (string.IsNullOrWhiteSpace(PoliticalStatus)) sb.Append($"{nameof(PoliticalStatus)}（学生政治面貌）\r\n");

                //学生籍贯
                if (string.IsNullOrWhiteSpace(NativePlace.ToString())) sb.Append($"{nameof(NativePlace)}（学生籍贯）\r\n");

                //学生户籍所在地
                if (string.IsNullOrWhiteSpace(ResidencePlace.ToString())) sb.Append($"{nameof(ResidencePlace)}（学生现户籍所在地）\r\n");

                //户籍性质
                if (string.IsNullOrWhiteSpace(ResidenceType)) sb.Append($"{nameof(ResidenceType)}（学生户口性质）\r\n");

                //学生健康状况
                if (string.IsNullOrWhiteSpace(StudentHealthCondition)) sb.Append($"{nameof(StudentHealthCondition)}（学生健康状况）\r\n");

                //学生现居住地
                if (string.IsNullOrWhiteSpace(StudentCurrentPlace.ToString())) sb.Append($"{nameof(StudentCurrentPlace)}（学生现居住地）\r\n");

                //学生现居住地详细地址
                if (string.IsNullOrWhiteSpace(StudentDetailAddress)) sb.Append($"{nameof(StudentDetailAddress)}（学生现居住地详细地址）\r\n");

                //学生现住址邮编
                if (string.IsNullOrWhiteSpace(PostCode)) sb.Append($"{nameof(PostCode)}（学校现住址邮编）\r\n");

                //学生电子邮件可以为空
                //if (string.IsNullOrWhiteSpace(StudentEmail)) sb.Append($"{nameof(StudentEmail)}\r\n");

                //学生电话号码可以为空
                //if (string.IsNullOrWhiteSpace(StudentPhoneCode)) sb.Append($"{nameof(StudentPhoneCode)}\r\n");

                //监护人姓名
                if (string.IsNullOrWhiteSpace(GuardianName)) sb.Append($"{nameof(GuardianName)}（学生监护人姓名）\r\n");

                //学生监护人单位
                if (string.IsNullOrWhiteSpace(GuardianCompany)) sb.Append($"{nameof(GuardianCompany)}（学生监护人单位）\r\n");

                //学生监护人身份证号
                if (string.IsNullOrWhiteSpace(GuardianCid)) sb.Append($"{nameof(GuardianCid)}（监护人身份证号）\r\n");

                //学生监护人电话号码
                if (string.IsNullOrWhiteSpace(GuardianPhoneCode)) sb.Append($"{nameof(GuardianPhoneCode)}（监护人电话号码）\r\n");

                //学生监护人与学生关系
                if (string.IsNullOrWhiteSpace(GuardianRelation)) sb.Append($"{nameof(GuardianRelation)}（监护人与学生的关系）\r\n");

                var result = sb.ToString();

                if (string.IsNullOrWhiteSpace(result)) return "";

                return $"以下字段未设置值：\r\n{result}";
            }
        }

        string studentGrade = "";
        /// <summary>
        /// 学生所在年级。
        /// </summary>
        public string StudentGrade
        {
            get { return studentGrade; }
            set
            {
                studentGrade = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentGrade)));
            }
        }

        string studentClass = "";
        /// <summary>
        /// 学生所在班级。
        /// </summary>
        public string StudentClass
        {
            get { return studentClass; }
            set
            {
                studentClass = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(studentClass)));
            }
        }

        string studentNumber = "";
        /// <summary>
        /// 学生在班级的编号。
        /// </summary>
        public string StudentNumber
        {
            get { return studentNumber; }
            set
            {
                studentNumber = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentNumber)));
            }
        }

        string studentName = "";
        /// <summary>
        /// 学生姓名。
        /// </summary>
        public string StudentName
        {
            get { return NameRule.Format(studentName); }
            set
            {
                studentName = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentName)));
            }
        }

        string studentOldName = "";
        /// <summary>
        /// 学生曾用名。
        /// </summary>
        public string StudentOldName
        {
            get { return NameRule.Format(studentOldName); }
            set
            {
                studentOldName = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentOldName)));
            }
        }

        string studentCid = "";
        /// <summary>
        /// 学生身份证号。
        /// </summary>
        public string StudentCid
        {
            get { return CIDRule.Format(studentCid); }
            set
            {
                studentCid = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentCid)));
            }
        }

        string schoolRoll = "";
        /// <summary>
        /// 学籍号。
        /// </summary>
        public string SchoolRoll
        {
            get { return SchoolRollRule.Format(schoolRoll); }
            set
            {
                schoolRoll = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(SchoolRoll)));
            }
        }

        string admissionDate = "";
        /// <summary>
        /// 入学年月。
        /// </summary>
        public string AdmissionDate
        {
            get { return admissionDate.Replace("年", "-").Replace("月", ""); }
            set
            {
                admissionDate = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(AdmissionDate)));
            }
        }

        string studentNation = "汉族";
        /// <summary>
        /// 学生民族。
        /// </summary>
        public string StudentNation
        {
            get { return studentNation; }
            set
            {
                studentNation = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentNation)));
            }
        }

        string politicalStatus = "团员";
        /// <summary>
        /// 学生政治面貌。
        /// </summary>
        public string PoliticalStatus
        {
            get { return politicalStatus; }
            set
            {
                politicalStatus = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(PoliticalStatus)));
            }
        }

        string nativePlace = "";
        /// <summary>
        /// 学生籍贯。
        /// </summary>
        public string NativePlace
        {
            get { return nativePlace; }
            set
            {
                var oldValue = nativePlace;
                nativePlace = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(NativePlace)));
                OnNativePlaceChanged(this, new PlaceChangedEventArgs()
                {
                    OldPlace = oldValue,
                    NewPlace = value,
                });
            }
        }

        public void SetNativePlace(string text)
        {
            nativePlace = text;
        }

        public event EventHandler<PlaceChangedEventArgs> NativePlaceChanged;

        protected void OnNativePlaceChanged(object sender, PlaceChangedEventArgs e)
        {
            if (NativePlaceChanged != null)
            {
                NativePlaceChanged(sender, e);
            }
        }

        string residencePlace = "";
        /// <summary>
        /// 学生现户籍所在地。
        /// </summary>
        public string ResidencePlace
        {
            get { return residencePlace; }
            set
            {
                var oldValue = residencePlace;
                residencePlace = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(ResidencePlace)));
                OnResidencePlaceChanged(this,
                    new PlaceChangedEventArgs()
                    {
                        OldPlace = oldValue,
                        NewPlace = value,
                    });
            }
        }

        public void SetResidencePlace(string text)
        {
            residencePlace = text;
        }

        public event EventHandler<PlaceChangedEventArgs> ResidencePlaceChanged;

        protected void OnResidencePlaceChanged(object sender, PlaceChangedEventArgs e)
        {
            if (ResidencePlaceChanged != null)
            {
                ResidencePlaceChanged(sender, e);
            }
        }

        string residenceType = "";
        /// <summary>
        /// 学生户口性质。
        /// </summary>
        public string ResidenceType
        {
            get { return residenceType; }
            set
            {
                residenceType = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(ResidenceType)));
            }
        }

        string studentHealthCondition = "";
        /// <summary>
        /// 学生健康状况。
        /// </summary>
        public string StudentHealthCondition
        {
            get { return this.studentHealthCondition; }
            set
            {
                this.studentHealthCondition = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentHealthCondition)));
            }
        }

        string studentCurrentPlace = "";
        /// <summary>
        /// 学生现居住地。
        /// </summary>
        public string StudentCurrentPlace
        {
            get { return studentCurrentPlace; }
            set
            {
                var oldValue = studentCurrentPlace;
                studentCurrentPlace = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(studentCurrentPlace)));
                OnStudentCurrentPlaceChanged(this, new PlaceChangedEventArgs()
                {
                    OldPlace = oldValue,
                    NewPlace = value,
                });
            }
        }

        public event EventHandler<PlaceChangedEventArgs> StudentCurrentPlaceChanged;

        protected void OnStudentCurrentPlaceChanged(object sender, PlaceChangedEventArgs e)
        {
            if (StudentCurrentPlaceChanged != null)
            {
                StudentCurrentPlaceChanged(sender, e);
            }
        }

        public void SetStudentCurrentPlace(string text)
        {
            studentCurrentPlace = text;
        }

        string studentDetailAddress = "";
        /// <summary>
        /// 学生现详细住址。
        /// </summary>
        public string StudentDetailAddress
        {
            get { return studentDetailAddress; }
            set
            {
                this.studentDetailAddress = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentDetailAddress)));
            }
        }

        string postCode = "";
        /// <summary>
        /// 邮政编码。
        /// </summary>
        public string PostCode
        {
            get { return postCode; }
            set
            {
                postCode = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(PostCode)));
            }
        }

        string studentEmail = "";
        /// <summary>
        /// 学生电子邮件地址。
        /// </summary>
        public string StudentEmail
        {
            get { return studentEmail; }
            set
            {
                studentEmail = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentEmail)));
            }
        }

        string studentPhoneCode = "";
        /// <summary>
        /// 学生电话号码。
        /// </summary>
        public string StudentPhoneCode
        {
            get { return studentPhoneCode; }
            set
            {
                studentPhoneCode = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(StudentPhoneCode)));
            }
        }

        string guardianName = "";
        /// <summary>
        /// 监护人姓名。
        /// </summary>
        public string GuardianName
        {
            get { return guardianName; }
            set
            {
                guardianName = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(GuardianName)));
            }
        }

        string guardianCompany = "自主择业";
        /// <summary>
        /// 监护人工作单位。
        /// </summary>
        public string GuardianCompany
        {
            get { return guardianCompany; }
            set
            {
                guardianCompany = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(GuardianCompany)));
            }
        }

        string guardianRelation = "";
        /// <summary>
        /// 监护人与学生关系。
        /// </summary>
        public string GuardianRelation
        {
            get { return guardianRelation; }
            set
            {
                guardianRelation = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(GuardianRelation)));
            }
        }

        string guardianCid = "";
        /// <summary>
        /// 监护人身份证号。
        /// </summary>
        public string GuardianCid
        {
            get { return CIDRule.Format(guardianCid); }
            set
            {
                guardianCid = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(GuardianCid)));
            }
        }

        string guardianPhoneCode = "";
        /// <summary>
        /// 监护人电话号码。
        /// </summary>
        public string GuardianPhoneCode
        {
            get { return guardianPhoneCode; }
            set
            {
                guardianPhoneCode = value;
                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(GuardianPhoneCode)));
            }
        }

        public class PlaceChangedEventArgs : EventArgs
        {
            public string OldPlace { get; set; }
            public string NewPlace { get; set; }
        }
    }
}
